﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Cards
{
    class Board
    {
        #region Variables

        //textures for the player chips
        Texture2D p1Chip, p2Chip;

        //Sprite sheet for cards
        Texture2D cardSet;

        //Texture for free space
        Texture2D freeCard;

        public enum chipState
        {
            empty = 0, //nothing on board
            p1 = 1, //player 1 chip on board
            p2 = 2 //player 2 chip on board
        };


        //and array of chip states
        public static chipState[] chips;

        //Dictionary<int,string> cardPics = new Dictionary<int,string>();

        #endregion

        public Board()
        {
            chips = new chipState[101];

            //cardPics
        }

        public void LoadContent(ContentManager Content)
        {
            p1Chip = Content.Load<Texture2D>("player1Chip");
            p2Chip = Content.Load<Texture2D>("player2Chip");

            cardSet = Content.Load<Texture2D>("Cards");
            freeCard = Content.Load<Texture2D>("Free_Space");
        }

        public void Draw(SpriteBatch spriteBatch, Input playerInput)
        {
            spriteBatch.Begin();

            //
            //draw the cards for the board
            //
            int boardLoc = 1;
            Vector2 value = new Vector2();

            for (int y = 1; y <= 10; y++)
            {
                for (int x = 1; x <= 10; x++)
                {
                    //pos = Board.FindCoordinate(boardLoc);
                    value = Board.FindCoordinate13(Game1.boardDeck.deck[boardLoc]);

                    if(Game1.boardDeck.deck[boardLoc] == 53)//if free
                    {
                        spriteBatch.Draw(freeCard, new Rectangle(x * 65,y * 40,freeCard.Width, freeCard.Height), new Rectangle(0, 0, 65, 40), Color.White);
                        boardLoc++;
                    }
                    //else if (Game1.boardDeck.deck[boardLoc] == 39)//if king clubs 13
                    //{ }
                    else
                    {
                        //draw the cards
                        spriteBatch.Draw(cardSet, new Rectangle(x * 65, y * 40, 65, 40), new Rectangle(((int)value.X - 1) * 65, ((int)value.Y - 1) * 40, 65, 40), Color.White);
                        boardLoc++;
                    }
                    
                }
            }//end draw board

            //
            //Draw hands
            //
            //draws player one hand
            for (int i = 1; i <= 6; i++)
            {
                value = Board.FindCoordinate13(Game1.player1Hand[i]);
                spriteBatch.Draw(cardSet, new Rectangle(i * 65, 480, 65, 40), new Rectangle(((int)value.X - 1) * 65, ((int)value.Y - 1) * 40, 65, 40), Color.White);
            }

            //draws player two hand
            for (int i = 1; i <= 6; i++)
            {
                value = Board.FindCoordinate13(Game1.player2Hand[i]);
                spriteBatch.Draw(cardSet, new Rectangle(i * 65, 520, 65, 40), new Rectangle(((int)value.X - 1) * 65, ((int)value.Y - 1) * 40, 65, 40), Color.White);
            }//end draw hands

            //
            //loop through all spots on the board drawing the appropriate chips
            //
            for (int loc = 1; loc <= 100; loc++)
            {
                Vector2 location = new Vector2();
                location = (FindCoordinate10(loc));

                //x = FindXCoordinate(loc);


                //draw
                if (chips[loc] == chipState.p1)
                {
                    spriteBatch.Draw(p1Chip, new Vector2((location.X * 65) + 20, (location.Y * 40) + 8), new Rectangle(0, 0, p1Chip.Width, p1Chip.Height), Color.White, 0, new Vector2(0, 0), .5f, 0, 0);
                }
                else
                {
                    if (chips[loc] == chipState.p2)
                    {
                        spriteBatch.Draw(p2Chip, new Vector2((location.X * 65) + 20, (location.Y * 40) + 8), new Rectangle(0, 0, p1Chip.Width, p1Chip.Height), Color.White, 0, new Vector2(0, 0), .5f, 0, 0);
                    }
                }
            }//end draw chips


            #region foreach stuff
            /*
            foreach(chipState chip in chips)
            {
                if (chip == chipState.p1)
                {
                    //this.EndDraw();
                    spriteBatch.Draw(p1Chip, new Vector2(playerInput.cursorPosition[playerInput.position].X, playerInput.cursorPosition[playerInput.position].Y), new Rectangle(0, 0, p1Chip.Width, p1Chip.Height), Color.White, 0, new Vector2(0, 0), .5f, 0, 0);
                }
                if (chip == chipState.p2)
                {
                    spriteBatch.Draw(p2Chip, new Vector2(playerInput.cursorPosition[playerInput.position].X, playerInput.cursorPosition[playerInput.position].Y), new Rectangle(0, 0, p1Chip.Width, p1Chip.Height), Color.White, 0, new Vector2(0, 0), .5f, 0, 0);
                }
            }
            */
            #endregion

            spriteBatch.End();
        }

        public static Vector2 FindCoordinate10(int loc)
        {
            //int x = 0; int y = 0;
            Vector2 vec = new Vector2();

            //find the correct x location given the position
            if ((loc % 10) == 0)//far right case
                vec.X = 10;
            else
                vec.X = loc % 10;


            //find the right y component with a given position
            if (loc >= 1 && loc <= 10)
                vec.Y = 1;
            if (loc >= 11 && loc <= 20)
                vec.Y = 2;
            if (loc >= 21 && loc <= 30)
                vec.Y = 3;
            if (loc >= 31 && loc <= 40)
                vec.Y = 4;
            if (loc >= 41 && loc <= 50)
                vec.Y = 5;
            if (loc >= 51 && loc <= 60)
                vec.Y = 6;
            if (loc >= 61 && loc <= 70)
                vec.Y = 7;
            if (loc >= 71 && loc <= 80)
                vec.Y = 8;
            if (loc >= 81 && loc <= 90)
                vec.Y = 9;
            if (loc >= 91 && loc <= 101)
                vec.Y = 10;

            return vec;
        }
        public static Vector2 FindCoordinate13(int loc)
        {
            //int x = 0; int y = 0;
            Vector2 vec = new Vector2();

            //find the correct x location given the position
            if ((loc % 13) == 0)//far right case
                vec.X = 13;
            else
                vec.X = loc % 13;


            //find the right y component with a given position
            if (loc >= 1 && loc <= 13)
                vec.Y = 1;
            if (loc >= 14 && loc <= 26)
                vec.Y = 2;
            if (loc >= 27 && loc <= 39)
                vec.Y = 3;
            if (loc >= 40 && loc <= 52)
                vec.Y = 4;

            return vec;
        }
    }
}
